package com.vdg.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import com.vdg.data.AppInformation;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

public class Utilities {
	private final static String PRE_NAME = "cfgprefrence";
	private final static String APP_LOCKED_KEY = "app_locked";
	private final static String PATTERN_KEY = "pattern";

	public static boolean saveStringToSharedPreferences(String save,
			Context context, String key) {
		SharedPreferences mPrefrence = context
				.getSharedPreferences(PRE_NAME, 0);
		Editor mPrefrenceEdit = mPrefrence.edit();
		if (mPrefrenceEdit == null) {
			return false;
		}
		mPrefrenceEdit.putString(key, save);
		return mPrefrenceEdit.commit();
	}

	public static String getStringFromSharedPreferences(String key,
			Context context) {
		SharedPreferences mPrefrence = context
				.getSharedPreferences(PRE_NAME, 0);
		if (mPrefrence == null) {
			return "";
		}
		return mPrefrence.getString(key, "");
	}

	public static ArrayList<String> getListAppLocked(Context context) {
		String saveString = getStringFromSharedPreferences(APP_LOCKED_KEY,
				context);
		ArrayList<String> listResult = new ArrayList<String>();
		if (saveString == null || saveString.equals("")) {
			return listResult;
		} else {
			if (saveString.contains("-")) {
				String[] listElement = saveString.split("-");
				for (String element : listElement) {
					if (element != null && !element.equals("")) {
						listResult.add(element);
					}
				}
				return listResult;
			} else {
				listResult.add(saveString);
				return listResult;
				// return new LinkedList(Arrays.asList(result));
			}
		}
	}

	public static void savePatternToSharedPreferences(Context context,
			char[] pattern) {
		String strPattern = String.valueOf(pattern);
		saveStringToSharedPreferences(strPattern, context, PATTERN_KEY);
	}

	public static char[] loadPatternFromSharePreferences(Context context) {
		String strPattern = getStringFromSharedPreferences(PATTERN_KEY, context);
		if (strPattern == null || strPattern.equals("")) {
			return null;
		} else {
			return strPattern.toCharArray();
		}
	}

	public static void saveListAppLocked(Context context,
			List<String> applockeds) {
		String strListApp = "";
		for (String applocked : applockeds) {
			if (strListApp.equals("")) {
				strListApp = applocked;
			} else {
				strListApp = strListApp + "-" + applocked;
			}
		}
		saveStringToSharedPreferences(strListApp, context, APP_LOCKED_KEY);
	}

	public static boolean isLocked(String appPackageName, Context context) {
		ArrayList<String> listApplocked = getListAppLocked(context);
		for (String packageName : listApplocked) {
			if (packageName.equals(appPackageName)) {
				return true;
			}
		}
		return false;
	}

	public static void removeAppLocker(String appPackageName, Context context) {
		ArrayList<String> listApplocked = getListAppLocked(context);
		if (listApplocked.contains(appPackageName)) {
			listApplocked.remove(appPackageName);
		}
		saveListAppLocked(context, listApplocked);
	}

	public static String getAppName(Context context, String packageName) {
		final PackageManager pm = context.getPackageManager();
		ApplicationInfo ai;
		try {
			ai = pm.getApplicationInfo(packageName, 0);
		} catch (final NameNotFoundException e) {
			ai = null;
		}
		final String applicationName = (String) (ai != null ? pm
				.getApplicationLabel(ai) : "(unknown)");
		return applicationName;
	}

	public static AppInformation getAppInformation(String packageName,
			Context context) {
		try {
			ApplicationInfo app = context.getPackageManager()
					.getApplicationInfo(packageName, 0);
			String name = app.loadLabel(context.getPackageManager()).toString();
			return new AppInformation(app, true, name);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}
}
